<template>
    <div class="login-wrap">
        <!-- vue-particles https://www.cnblogs.com/tangwei89/p/11512804.html -->
        <vue-particles
            color="#fff"
            :particleOpacity="0.7"
            :particlesNumber="60"
            shapeType="circle"
            :particleSize="4"
            linesColor="#fff"
            :linesWidth="1"
            :lineLinked="true"
            :lineOpacity="0.4"
            :linesDistance="150"
            :moveSpeed="2"
            :hoverEffect="true"
            hoverMode="grab"
            :clickEffect="true"
            clickMode="push"
        >
        </vue-particles>
        <div class="ms-login">
            <div class="ms-title">智慧建筑后台管理系统</div>
            <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="username">
                    <el-input v-model="param.username" placeholder="username">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="password" v-model="param.password" @keyup.enter.native="submitForm()">
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="role">
                    <div class="prefix">
                        <el-button slot="prepend" icon="el-icon-lx-group"></el-button>
                    </div>
                    <el-select v-model="param.role" placeholder="role">
                        <el-option label="设计方" value="designer"></el-option>
                        <el-option label="施工方" value="builder"></el-option>
                        <el-option label="监理" value="watcher"></el-option>
                        <el-option label="业主" value="boss"></el-option>
                    </el-select>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm()">登录</el-button>
                </div>
                <a href="#" class="login-tips">忘记密码?</a>
                <a href="#" class="login-tips top-left" @click.prevent="register">注册</a>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    data: function() {
        return {
            param: {
                username: '',
                password: '',
                role: ''
            },
            rules: {
                username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
                role: [{ required: true, message: '请选择身份', trigger: 'blur' }]
            }
        };
    },
    methods: {
        submitForm() {
            this.$refs.login.validate(valid => {
                if (valid) {
                    // this.$axios({
                    //     method: 'post',
                    //     url: 'http://localhost:8080/user/login',
                    //     data: this.param,
                    // })
                    this.$axios({
                        method: 'get',
                        url: 'http://localhost:8080/user/login',
                        params: this.param
                    })
                        // this.$axios.post('http://localhost:8080/user/login', this.param)
                        // this.$axios.get("/movie/top250")
                        // .then(res => {
                        //     console.log(res);
                        //     this.$message({
                        //         message: '登录成功',
                        //         type: 'success',
                        //         center: true
                        //     });
                        //     // localStorage.setItem('ms_username', this.param.username);
                        //     localStorage.setItem('username', this.param.username);
                        //     this.$router.push('/');
                        // })
                        .then(res => {
                            console.log(res);
                            if (res.data.Msg.flag) {
                                sessionStorage.setItem("role",res.data.Userinfo.role)   
                                this.$router.push({
                                    path: '/dashboard'
                                });
                            }
                        })
                        .catch(err => {
                            this.$message({
                                message: '登录失败',
                                type: 'error',
                                showClose: true,
                                center: true
                            });
                        });
                } else {
                    this.$message({
                        message: '用户名或密码不正确！',
                        type: 'error',
                        showClose: true,
                        center: true
                    });
                }
            });
        },
        register() {
            this.$router.push('/register');
        }
    }
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    /* background-image: url(../../assets/img/login-bg.jpg); */
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}
.ms-login {
    z-index: 99;
    position: absolute;
    left: 50%;
    top: 50%;
    width: 450px;
    margin-left: -225px;
    margin-top: -160px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.05);
    overflow: hidden;
}
.prefix {
    display: inline-block;
    width: 45px;
    height: 31px;
    border-color: transparent;
    background-color: transparent;
    color: inherit;
    margin-right: -1px;
}
.el-select {
    width: 344px;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
.top-left {
    margin-left: 305px;
}
</style>
